using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components;
using Workflows.Components.DB.Properties;

namespace Workflows.Components.DB.Orm
{
    /// <summary></summary>
    [Serializable]
    public class OrmComponent:WorkflowComponentBase
    {
        #region props
        private OrmInstruction _Instruction;
        private OrmOutput _Output;

        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(OrmInstruction) value; }
        }

        public override ResultBase Output
        {
            get
            {
                this._Output =
                    new OrmOutput(this._Instruction.OwnerComponentID, Guid.Empty, this._Instruction.BindableObject);
                return _Output;
            }
            set { _Output = (OrmOutput) value; }
        }
        /// <summary></summary>
        public string ConnStr
        {
            get { return this._Instruction.ConnStr; }
            set { this._Instruction.ConnStr = value; }
        }
        /// <summary></summary>
        public string TblName
        {
            get { return this._Instruction.TblName; }
            set { this._Instruction.TblName = value; }
        }
        /// <summary></summary>
        public string PK
        {
            get { return this._Instruction.PK; }
            set { this._Instruction.PK = value; }
        }
        /// <summary></summary>
        public Type PkDataType
        {
            get { return this._Instruction.PkDataType; }
            set { this._Instruction.PkDataType = value; }
        }
        /// <summary></summary>
        public bool IsIdentity
        {
            get { return this._Instruction.IsIdentity; }
            set { this._Instruction.IsIdentity = value; }
        }
        /// <summary></summary>
        public OrmActionType ActionType
        {
            get { return this._Instruction.ActionType; }
            set { this._Instruction.ActionType = value; }
        }
        /// <summary></summary>
        public string BindableObjAssemblyFileName
        {
            get { return this._Instruction.BindableObjAssemblyFileName; }
            set { this._Instruction.BindableObjAssemblyFileName = value; }
        }
        /// <summary></summary>
        public string BindableObjTypeFullName
        {
            get { return this._Instruction.BindableObjTypeFullName; }
            set { this._Instruction.BindableObjTypeFullName = value; }
        }
        #endregion
        /// <summary></summary>
        public OrmComponent():base()
        {
            this.ComponentName = "ORM";
            this.Description = "Synchronize table data and object instance in memory";
            this.ComponentIcon = Resources.ARPWrapper_221;
            this.ComponentImage = Resources.ODD_JPG;
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Database_Tasks.ToString();

            this._Instruction =
                new OrmInstruction(this.ComponentID, Guid.Empty, string.Empty, string.Empty, string.Empty, typeof (int),
                                   false, OrmActionType.Insert, string.Empty, typeof (string).FullName, string.Empty);
            this._Output =
                new OrmOutput(this.ComponentID, Guid.Empty, null);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnectionString", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TblName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PkField", this.PK);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PkType", this.PkDataType.FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IsIdentity", this.IsIdentity.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Action", ((int) this.ActionType).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BindableObjectAssemblyName", this.BindableObjAssemblyFileName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BindableObjectTypeName",this.BindableObjTypeFullName);

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                OrmComponent component = base.Instantiate(xNode) as OrmComponent;

                component.ConnStr = xNode.Attributes.GetNamedItem("ConnectionString").Value;
                component.TblName = xNode.Attributes.GetNamedItem("TableName").Value;
                component.PK = xNode.Attributes.GetNamedItem("PkField").Value;
                component.PkDataType = Type.GetType(xNode.Attributes.GetNamedItem("PkType").Value);
                component.IsIdentity = bool.Parse(xNode.Attributes.GetNamedItem("IsIdentity").Value);
                component.ActionType = (OrmActionType) int.Parse(xNode.Attributes.GetNamedItem("Action").Value);
                component.BindableObjAssemblyFileName =
                    xNode.Attributes.GetNamedItem("BindableObjectAssemblyName").Value;
                component.BindableObjTypeFullName = xNode.Attributes.GetNamedItem("BindableObjectTypeName").Value;

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}
